﻿



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using lite;
using Csla;
using Csla.Data;
using Csla.Core;
using System.Collections.ObjectModel;
using OpenExpressApp.ORM;
using OpenExpressApp.MetaAttribute;


namespace OrderLibrary
{
    [Serializable]
    public partial class OrderItems : GBusinessListBase<OrderItems, OrderItem>
    {
        #region  Factory Methods

        private OrderItems()
        {
            /* Require use of factory methods */
            AllowNew = true;
            /* ??????????????AutoUI????????????????????????????  */
            //AllowEdit = false;
        }


        internal static OrderItems GetChild(Order parent)
        {
            return DataPortal.FetchChild<OrderItems>(parent);
        }

        public static OrderItems NewChild()
        {
            return DataPortal.CreateChild<OrderItems>();
        }

        protected override object AddNewCore()
        {
            var item = OrderItem.NewChild();
            Add(item);
            return item;
        }

        #endregion

        #region  Data Access

        private void Child_Fetch(Order parent)
        {

            RaiseListChangedEvents = false;

            using (var ctx = ConnectionManager<SqlConnection>.GetManager("BusinessDBName"))
            {
                IDb db = DbFactory.Instance.GetDb(ctx.Connection);
                IQuery q = db.Query();
                q.Constrain("OrderId").Equal(parent.Id);
                foreach (OrderItem c in db.Select<OrderItem>(q))
                {
                    this.Add(OrderItem.GetChild(parent, c));
                }
            }

            RaiseListChangedEvents = true;
        }

        #endregion
        
}

    [Serializable]
    [Table]
    [BusinessObject("23B2E660-BFF1-40C2-B285-D5FF0C16D9F3"), Label("订单项")]
    public partial class OrderItem : GBusinessBase<OrderItem> 
    {        
        #region  Business Methods

        private static PropertyInfo<Guid> IdProperty =
            RegisterProperty(new PropertyInfo<Guid>("Id"));
        [System.ComponentModel.DataObjectField(true, true)]
        [Column, PK][EntityProperty]
        public Guid Id
        {
            get { return GetProperty(IdProperty); }
            set { SetProperty(IdProperty, value); }
        }
        
	
        private static PropertyInfo<Guid> OrderIdProperty =
         RegisterProperty(new PropertyInfo<Guid>("OrderId"));
        [Column] [EntityProperty][Lookup("Order")]
        public Guid OrderId
        {
            get { return GetProperty(OrderIdProperty); }
            set { SetProperty(OrderIdProperty, value); }
        }
        [NotUndoable()]
        private Order order;
        public Order Order
        {
            get { return order; }
            set { order = value; }
        }

        private static PropertyInfo<string> NameProperty =
            RegisterProperty(new PropertyInfo<string>("Name"));
        [Column]
        [EntityProperty]
        [ShowInListAttribute, Label("产品名称")]
        public string Name
        {
            get { return GetProperty(NameProperty); }
            set { SetProperty(NameProperty, value); }
        }

        private static PropertyInfo<double> PriceProperty =
            RegisterProperty(new PropertyInfo<double>("Price"));
        [Column]
        [EntityProperty]
        [ShowInListAttribute, Label("单价")]
        public double Price
        {
            get { return GetProperty(PriceProperty); }
            set 
            { 
                SetProperty(PriceProperty, value);
                PropertyHasChanged("Money");  //金额UI自动更新
            }
        }

        private static PropertyInfo<double> AmountProperty =
            RegisterProperty(new PropertyInfo<double>("Amount"));
        [Column]
        [EntityProperty]
        [ShowInListAttribute, Label("数量")]
        public double Amount
        {
            get { return GetProperty(AmountProperty); }
            set 
            { 
                SetProperty(AmountProperty, value);
                PropertyHasChanged("Money");//金额UI自动更新
            }
        }

        [EntityProperty]
        [ShowInListAttribute, Label("金额")]
        public double Money
        {
            get { return Price*Amount; }
        }

        #endregion
        
        #region  Factory Methods

        internal static OrderItem NewChild()
        {
            return DataPortal.CreateChild<OrderItem>();
        }
        
        internal static OrderItem GetChild(Order order, OrderItem c)
        {
            c.Order = order;
            return DataPortal.FetchChild<OrderItem>(c);
        }


        private OrderItem()
        {             /* require use of factory methods */         }


        #endregion

        #region  Data Access

        protected override void Child_Create()
        {
            LoadProperty(IdProperty, Guid.NewGuid());
            ValidationRules.CheckRules();
        }

        private void Child_Fetch(OrderItem data)
        {
            LoadProperty(IdProperty, data.ReadProperty(IdProperty));
            LoadProperty(OrderIdProperty, data.ReadProperty(OrderIdProperty));
            Order = data.Order;
            LoadProperty(NameProperty, data.ReadProperty(NameProperty));
            LoadProperty(PriceProperty, data.ReadProperty(PriceProperty));
            LoadProperty(AmountProperty, data.ReadProperty(AmountProperty));
        }

        private void Child_Insert(BusinessBase parent)
        {
            using (var ctx = ConnectionManager<SqlConnection>.GetManager("BusinessDBName"))
            {
                IDb db = DbFactory.Instance.GetDb(ctx.Connection);
                db.Insert(this);
            }
        }
        private void Child_Update(BusinessBase parent)
        {
            using (var ctx = ConnectionManager<SqlConnection>.GetManager("BusinessDBName"))
            {
                IDb db = DbFactory.Instance.GetDb(ctx.Connection);
                db.Update(this);
            }
        }


        private void Child_DeleteSelf(BusinessBase parent)
        {
            using (var ctx = ConnectionManager<SqlConnection>.GetManager("BusinessDBName"))
            {
                IDb db = DbFactory.Instance.GetDb(ctx.Connection);
                db.Delete(this);
            }
        }

        #endregion

        #region  Validation Rules

        protected override void AddBusinessRules()
        {

        }

        #endregion

    }
}
